package org.jflame.commons.crypto;

import java.lang.reflect.InvocationTargetException;
import java.security.Provider;
import java.security.Security;

public final class BC {

    /**
     * 如果有BouncyCastle包注册Provider
     * 
     * @throws EncryptException
     */
    public static Provider addProvider() throws EncryptException {
        try {
            Provider bc = Security.getProvider("BC");
            if (bc == null) {
                Class<?> clz = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
                bc = (Provider) clz.getDeclaredConstructor()
                        .newInstance();
                Security.addProvider(bc);
            }
            return bc;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | NoSuchMethodException | SecurityException e) {
            throw new EncryptException("An exception occurred while add bouncyCastle provider");
        }

    }

}
